package controller

import (
	"bufio"
	"github.com/gin-gonic/gin"
	"os"
	"strconv"
	"strings"
)

type QsCtl struct {
}

var Qs = QsCtl{}

var qinshanMap = readQinShanMap()

type Point struct {
	Lat float64 `json:"lat"`
	Lon float64 `json:"lon"`
}

func (this *QsCtl) Test(c *gin.Context) {
	//	接受
	var point Point
	c.ShouldBindJSON(&point)
	lat := int(point.Lat * 1000)
	lon := int(point.Lon * 1000)
	_, ok := qinshanMap[[2]int{lat, lon}]
	if ok {
		c.JSON(200, gin.H{
			"status": 0,
			"msg":    "在陆地上",
		})
	} else {
		c.JSON(200, gin.H{
			"status": 1,
			"msg":    "在水上",
		})
	}
}

func readQinShanMap() map[[2]int]struct{} {
	m := make(map[[2]int]struct{}, 600000)
	fileNames := []string{"qinshan1.txt", "qinshan2.txt"}
	for _, fileName := range fileNames {
		//m1 := readLandTxt("./public/image/0.05/"+fileName)
		f1, _ := os.Open("./public/image/" + fileName)
		scanner := bufio.NewScanner(f1)
		for scanner.Scan() {
			line := scanner.Text()
			lat, _ := strconv.ParseFloat(strings.Split(line, ",")[0], 64)
			lon, _ := strconv.ParseFloat(strings.Split(line, ",")[1], 64)
			m[[2]int{
				int(lat),
				int(lon),
			}] = struct{}{}
		}
		f1.Close()
	}
	return m
}
